package com.mall.shopping.services;/*
 *@author     :zhangxiaojun
 *@date       :2021/8/2517:12
 *@description: some description
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IGoodsService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class IGoodsServiceImpl implements IGoodsService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;


    /*@Override
    public AllProductResponse selectGoods(AllProductRequest allProductRequest) {
        AllProductResponse allProductResponse = new AllProductResponse();
        try {
            PageHelper.startPage(allProductRequest.getPage(), allProductRequest.getSize());
            List<Item> itemList = new ArrayList<>();
            if (!allProductRequest.getSort().equals("") && !allProductRequest.getSort().equals(null)) {
                String orderCol = "price";
                String orderDir = "asc";
                if (allProductRequest.getSort().equals("1")) {
                    orderDir = "asc";
                } else if (allProductRequest.getSort().equals("-1")) {
                    orderDir = "desc";
                }
                itemList = itemMapper.selectItemFront(allProductRequest.getCid(), orderCol, orderDir, allProductRequest.getPriceGt(), allProductRequest.getPriceLte());
            } else {
                Example itemExample = new Example(Item.class);
                Example.Criteria criteria = itemExample.createCriteria();
                if (allProductRequest.getPriceGt() != null && !"".equals(allProductRequest.getPriceGt()) && allProductRequest.getPriceLte() != null && !"".equals(allProductRequest.getPriceLte())) {
                    criteria.andBetween("price", allProductRequest.getPriceGt(), allProductRequest.getPriceLte());
                }
                itemList = itemMapper.selectByExample(itemExample);
            }
            List<ProductDto> productDtoList = productConverter.items2Dto(itemList);
            PageInfo<ProductDto> pageInfo = new PageInfo<>(productDtoList);
            long total = pageInfo.getTotal();
            allProductResponse.setProductDtoList(productDtoList);
            allProductResponse.setTotal(total);
            allProductResponse.setCode("000000");
            allProductResponse.setMsg("成功");
        } catch (Exception e) {
            log.error("IGoodsServiceImpl.selectGoods occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }
        return allProductResponse;
    }*/

    @Override
    public AllProductResponse selectGoods(AllProductRequest allProductRequest) {
        AllProductResponse response = new AllProductResponse();
        try {
            allProductRequest.requestCheck();
            PageHelper.startPage(allProductRequest.getPage(), allProductRequest.getSize());
            Example example = new Example(Item.class);
            if ("1".equals(allProductRequest.getSort())) {
                example.setOrderByClause("price asc");
            } else if ("-1".equals(allProductRequest.getSort())) {
                example.setOrderByClause("price desc");
            }
            Example.Criteria criteria = example.createCriteria();
            if (allProductRequest.getPriceLte() != null) {
                criteria.andLessThan("price", allProductRequest.getPriceLte());
            }
            if (allProductRequest.getPriceGt() != null) {
                criteria.andGreaterThan("price", allProductRequest.getPriceGt());
            }
            if (allProductRequest.getCid() != null) {
                criteria.andEqualTo("cid", allProductRequest.getCid());
            }
            List<Item> items = itemMapper.selectByExample(example);
            List<ProductDto> productDtos = productConverter.items2Dto(items);
            PageInfo<Item> itemPageInfo = new PageInfo<>(items);
            long total = itemPageInfo.getTotal();
            response.setProductDtoList(productDtos);
            response.setTotal(total);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return response;
    }

    @Override
    public RecommendResponse getRecommendGoods() {

        RecommendResponse recommendResponse = new RecommendResponse();
        try {
            Integer panelId = 6;
            Panel panel = panelMapper.selectByPrimaryKey(panelId);
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);
            panel.setPanelContentItems(panelContentItems);
            PanelDto panelDto = contentConverter.panen2Dto(panel);
            Set<PanelDto> panelDtoSet = new HashSet<>();
            panelDtoSet.add(panelDto);
            recommendResponse.setPanelContentItemDtos(panelDtoSet);
            recommendResponse.setCode("000000");
            recommendResponse.setMsg("成功");
        } catch (Exception e) {
            log.error("IGoodsServiceImpl.getRecommendGoods occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }
        return recommendResponse;
    }
}
